package com.gopay.solution.dao.impl;

import com.gopay.solution.dao.SsAcctSolutionRelDAO;
import com.gopay.solution.domain.po.SsAcctSolutionRel;
import com.gopay.solution.domain.po.SsTransPayChanDtl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 方案服务 交易付款渠道方案明细
 * 
 * @SsAcctGainDtlDAOImpl.java
 * @author liujy
 * @2013-3-26 上午11:05:44 www.gopay.com.cn Inc.All rights reserved.
 */

@SuppressWarnings({ "unchecked", "rawtypes" })
@Repository("ssTransPayChanDtlDAO")
public class SsTransPayChanDtlDAOImpl extends
        SolutionRuleDtlDAOImpl<SsTransPayChanDtl, Serializable> {

    @Resource(name = "ssAcctSolutionRelDAO")
    private SsAcctSolutionRelDAO ssAcctSolutionRelDAO;
    
    @Override
    public SsTransPayChanDtl getByPk(Serializable id) {
        return find(SsTransPayChanDtl.class, id);
    }

    @Override
    public List<SsTransPayChanDtl> listBySolutionId(long solutionId) {
        SsAcctSolutionRel asrel=ssAcctSolutionRelDAO.getBySoluId(solutionId);
        //是否个性化方案
        boolean isPersonal=asrel!=null;
        
        String hql = "from SsTransPayChanDtl where solutionId = " + solutionId;
        List<SsTransPayChanDtl> rsList=findByHql(hql);
        if(rsList!=null&&rsList.size()>0){
            for(SsTransPayChanDtl dtl :rsList){
                dtl.setPersonal(isPersonal);
            }
        }
        
        return rsList;
        
    }
    
    @Override
    public boolean delBySolutionId(long solutionId) {

        List<SsTransPayChanDtl> dtls = listBySolutionId(solutionId);
        if (dtls != null && dtls.size() > 0) {
            deleteAll(dtls);
        }
        return true;
    }
    
    public List<SsTransPayChanDtl> listBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
        String hql = "from SsTransPayChanDtl dtl where dtl.solutionId = :SolutionId and dtl.gopayIntTxnCd = :GopayIntTxnCd";
        Map params = new HashMap();
        params.put("SolutionId", solutionId);
        params.put("GopayIntTxnCd", intTxnCd);
        if(StringUtils.contains(intTxnCd,",")){
            hql="from SsTransPayChanDtl dtl where dtl.solutionId = :SolutionId and dtl.gopayIntTxnCd in( :GopayIntTxnCd)";
            params.put("GopayIntTxnCd", intTxnCd.split(","));
        }



        return findByHql(hql, params);
    }

    @Override
    public boolean delBySoluIdAndIntTxnCd(long solutionId, String intTxnCd) {
        List<SsTransPayChanDtl> dtls = listBySoluIdAndIntTxnCd(solutionId, intTxnCd);
        if (dtls != null && dtls.size() > 0) {
            deleteAll(dtls);
        }
        return true;
    }
}
